package cn.imvc.app.models;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.commons.SystemConfig;
import cn.imvc.app.domains.FundFlowType;
import cn.imvc.app.domains.OrderState;
import cn.imvc.app.entities.Account;
import cn.imvc.app.entities.Commission;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.Orders;
import cn.imvc.app.repositories.CommissionRepository;
import cn.imvc.data.domain.ViewRequest;

/**
 * 
 * @author wujx
 * @time 2016年8月3日
 *
 */
@Service("commissionModel")
public class CommissionModelImpl implements CommissionModel {
	@Autowired
	private AccountModel accountModes;
	@Autowired
	private SystemConfig systemConfig;
	@Autowired
	private FundFlowModel fundFlowModel;
	@Autowired
	private OrdersModel ordersModel;
	
	@Autowired
	private CommissionRepository commissionRepository;
	
	public void updateOrdersCommission(Orders order) {
		/**
		 * 订单分积分
		 */
		//订单的使用人
		Member member = order.getMember();
		//订单总钱数
		Integer totalMoney = order.getResultAmount()-order.getRefundAmount()-order.getFreightAmount();
		//如果有上级，给上级分佣
		Member supperMember = member.getParent();
		if(supperMember != null){
			//二级提成比例
			Integer secondPercent = 0 ;
			if(supperMember.getDealer().getDealerLevel().getLevel() != 1){
				secondPercent = systemConfig.getFirstLevelPercent()*2;
			}else{
				secondPercent = systemConfig.getFirstLevelPercent();
			}
			//二级提成积分
			Integer secondPoint = totalMoney*secondPercent/10000;
			Account supperAccout = supperMember.getAccount();
			//二级的佣金插入
			accountModes.retrySaveAccount(supperAccout.getId(), 0,0, secondPoint);
			//资金流水
			fundFlowModel.saveFundFlow(supperMember, secondPoint, order.getCode(), FundFlowType.PointsCommission.getValue(),member);
			//分佣表插入
			Commission commOne = new Commission();
			commOne.setMember(supperMember);
			commOne.setAccount(supperAccout);
			commOne.setType((byte) 12);
			commOne.setReferId(order.getId());
			commOne.setReferCode(order.getCode());
			commOne.setMoney(secondPoint);
			commOne.setRate(secondPercent);
			commOne.setCreatedAt(new Date());
			commOne.setFromMember(member);
			commissionRepository.save(commOne);
			
				/*//三级的佣金
				Member topMember = supperMember.getParent(); 
				if(topMember != null){
					//三级提成比例
					Integer thirdPercent = systemConfig.getSecondLevelPercent();
					//三级提成钱数
					Integer thirdMoney = totalMoney*thirdPercent/100;
					Account thirdAccout = topMember.getAccount();
					//三级的佣金插入
					accountModes.retrySaveAccount(thirdAccout.getId(), thirdMoney,thirdMoney, 0);
					//资金流水
					fundFlowModel.saveFundFlow(topMember, thirdMoney, order.getCode(), FundFlowType.OrderCommissions.getValue(),member);
					//分佣表插入
					Commission commTwo = new Commission();
					commTwo.setMember(topMember);
					commTwo.setAccount(thirdAccout);
					commTwo.setType((byte) 1);
					commTwo.setReferId(order.getId());
					commTwo.setReferCode(order.getCode());
					commTwo.setMoney(thirdMoney);
					commTwo.setRate(thirdPercent);
					commTwo.setCreatedAt(new Date());
					commTwo.setFromMember(member);
					commissionRepository.save(commTwo);
				}*/
		}
		
		/**
		 * 订单分钱
		 */
		/*//订单的使用人
		Member member = order.getMember();
		//订单总钱数
		Integer totalMoney = order.getResultAmount()-order.getRefundAmount()-order.getFreightAmount();
		//如果有上级，给上级分佣
		Member supperMember = member.getParent();
		if(supperMember != null){
			//二级提成比例
			Integer secondPercent = systemConfig.getFirstLevelPercent();
			//二级提成钱数
			Integer secondMoney = totalMoney*secondPercent/100;
			Account supperAccout = supperMember.getAccount();
			//二级的佣金插入
			accountModes.retrySaveAccount(supperAccout.getId(), secondMoney,secondMoney, 0);
			//资金流水
			fundFlowModel.saveFundFlow(supperMember, secondMoney, order.getCode(), FundFlowType.OrderCommissions.getValue(),member);
			//分佣表插入
			Commission commOne = new Commission();
			commOne.setMember(supperMember);
			commOne.setAccount(supperAccout);
			commOne.setType((byte) 1);
			commOne.setReferId(order.getId());
			commOne.setReferCode(order.getCode());
			commOne.setMoney(secondMoney);
			commOne.setRate(secondPercent);
			commOne.setCreatedAt(new Date());
			commOne.setFromMember(member);
			commissionRepository.save(commOne);
			
				//三级的佣金
				Member topMember = supperMember.getParent(); 
				if(topMember != null){
					//三级提成比例
					Integer thirdPercent = systemConfig.getSecondLevelPercent();
					//三级提成钱数
					Integer thirdMoney = totalMoney*thirdPercent/100;
					Account thirdAccout = topMember.getAccount();
					//三级的佣金插入
					accountModes.retrySaveAccount(thirdAccout.getId(), thirdMoney,thirdMoney, 0);
					//资金流水
					fundFlowModel.saveFundFlow(topMember, thirdMoney, order.getCode(), FundFlowType.OrderCommissions.getValue(),member);
					//分佣表插入
					Commission commTwo = new Commission();
					commTwo.setMember(topMember);
					commTwo.setAccount(thirdAccout);
					commTwo.setType((byte) 1);
					commTwo.setReferId(order.getId());
					commTwo.setReferCode(order.getCode());
					commTwo.setMoney(thirdMoney);
					commTwo.setRate(thirdPercent);
					commTwo.setCreatedAt(new Date());
					commTwo.setFromMember(member);
					commissionRepository.save(commTwo);
				}
		}*/
		order.setState(OrderState.Commissions.getValue());
		order.setUpdatedAt(new Date());
		ordersModel.update(order);
		
	}

	public Page<Commission> findAll(ViewRequest viewRequest) {
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("createdAt", "DESC");
		return commissionRepository.findAll("distinct c","Commission c left join c.member m",viewRequest);
	}

	public Page<Commission> findAllByMemberId(ViewRequest viewRequest, int id) {
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("createdAt", "DESC");
		viewRequest.getParams().put("_and_c_dot_member_dot_id_eq_number", id+"");
		return commissionRepository.findAll("c","Commission c",viewRequest);
	}
	
	public void save(Commission c){
		commissionRepository.save(c);
	}

	@Override
	public List<Commission> findAllByMemberAndCreatedAtBetween(Member member, Date startTime,Date endTime) {
		return commissionRepository.findByMemberAndCreatedAtBetween(member,startTime,endTime);
	}
}
